﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL
{
    public class LopBLL
    {
        static int id = 0;
        QuanLyDiemDataContext DB = new QuanLyDiemDataContext();
        public IQueryable SelectDes()
        {
            return DB.Lops.OrderByDescending(s => s.MaLop).AsQueryable();
        }

        public string Insert(LopDTO lopdto)
        {
            try
            {
                Lop lop = new Lop();
                lop.MaLop = (id++).ToString();
                lop.TenLop = lopdto.Tenlop;
                lop.MaKhoi = lopdto.Makhoi;
                lop.TrangThai = lopdto.Trangthai;

                DB.Lops.InsertOnSubmit(lop);
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string[] Gettenkhoi()
        {
        
            string[] data = new string[1000];
            data = (from t in DB.Khois
                    select t.TenKhoi).ToArray();
            return data;
        }

        public string Update(LopDTO lopdto)
        {
            try
            {
                Lop lop = DB.Lops.Where(s => s.MaLop == lopdto.Malop).FirstOrDefault();
                lop.TenLop = lopdto.Tenlop;
                lop.MaKhoi = lopdto.Makhoi;
                lop.TrangThai = lopdto.Trangthai;
           
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Delete()
        {
            try
            {
                DB.SubmitChanges();
                return "Thanh cong";

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public bool check(string tenlop)
        {
            var kq = DB.Lops.Where(lop=>lop.TenLop==tenlop).ToList();
            int kt = 0;
            kt = kq.Count();
            if (kt == 0)
                return true;
            else return false;
        }

    }
}
